package com.lanlian.cayman.utils;

import android.view.View;
import android.widget.CheckedTextView;

import java.util.List;

public class GridviewHelp {

    int select = -1;
    List<CheckedTextView> list;

    public void setList(List<CheckedTextView> list) {
        this.list = list;
        for (int i = 0; i < list.size(); i++) {
            setListener(list.get(i));
        }
    }

    private void setListener(CheckedTextView ctv) {
        ctv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < list.size(); i++) {
                    CheckedTextView ct = list.get(i);
                    if (ct != ctv) {
                        ct.setChecked(false);
                    } else {
                        select = i;
                    }
                }
                if (ctv.isChecked()) {
                    ctv.setChecked(false);
                } else {
                    ctv.setChecked(true);
                }
            }
        });
    }

    public int getSelectIndex() {
        return select;
    }

}
